package user.servlet;

import util.EmailUtil;
import util.ImageVerificationCode;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/PictureCodeServlet")
public class PictureCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ImageVerificationCode image=new ImageVerificationCode();
        //生成图片验证码
        BufferedImage bufferedImage=image.getImage();
        //将验证码保存到session
        request.getSession().setAttribute("picCode",image.getText());

        //响应图片给页面
        response.setContentType("image/jpeg");
        ImageIO.write(bufferedImage,"jpg",response.getOutputStream());

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
